package com.yunyi.web.controller.triage;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yunyi.common.core.domain.AjaxResult;
import com.yunyi.system.domain.ConditionData;
import com.yunyi.system.domain.EmrCase;
import com.yunyi.system.service.EmrCaseService;
import com.yunyi.system.service.EmrCaseSlideService;
import com.yunyi.system.service.SysPathologyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api(tags = "测试分诊管理 待分诊病例")
@RequestMapping("/triage/tbt")
@RestController
public class TriageManagerController {

    @Resource
    private EmrCaseService emrCaseService;

    @Resource
    private EmrCaseSlideService slideService;

    @Resource
    private SysPathologyService pathologyService;


    @GetMapping("getPathology")
    public AjaxResult getPathology(){
        return pathologyService.getPathology();
    }
    /**
     * 获取病例表和切片表的待分配及待撤回数量
     * wfd  病例待分配数量
     * tbw  病例待撤回数量
     * fzwfd    冰冻待分配数量
     * fztbw    冰冻待撤回数量
     * @return
     */
    @ApiOperation(value = "获取待分配和待撤回数量")
    @GetMapping("getTbt")
    public AjaxResult getTbt(){
        return emrCaseService.getTbt();
    }

    /**
     * 获取近七日内操作的分配信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取近期分配信息")
    @GetMapping("getLastOperation")
    public AjaxResult getLastOperation(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize",defaultValue = "19") Integer pageSize){
        return emrCaseService.getLastOperation(pageNum, pageSize);
    }

    /**
     * 根据条件查询待分配的病例信息
     * data.status
     * status=2时查询的是待分诊病例，
     * status=3时查询的是待撤回病例
     * status=4时查询的是被退回病例
     * status=7or8时查询的是已分诊病例
     * @data.pathology     条件1：病例类型 1.常规 2.细胞 3.冰冻 4.会诊
     * @data.sendHospital  条件2：申请医院
     * @data.caseNo        条件3：病理号
     * @data.name          条件4：病人姓名
     * @param pageNum       默认：第1页
     * @param pageSize      默认：显示19条
     * @return
     */
    @ApiOperation(value = "获取待分配病例信息")
    @PostMapping("getTbd")
    public AjaxResult getTbd(@RequestBody ConditionData data,
                             @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "19")Integer pageSize) {
        return emrCaseService.getTbd(data.getStatus(),data.getPathology(),data.getSendHospital(), data.getCaseNo(), data.getName(), pageNum, pageSize);
    }

    @ApiOperation(value = "分配病例")
    @PutMapping("allotCase")
    public AjaxResult allotCase(@RequestBody List<String> caseList) {
        return emrCaseService.allotCase(caseList);
    }

    @ApiOperation(value = "获取诊断报告单")
    @GetMapping("getDiagnosticReport/{id}")
    public AjaxResult getDiagnosticReport(@PathVariable("id") String id){
        return emrCaseService.getDiagnosticReport(id);
    }

    @ApiOperation(value = "获取切片信息")
    @GetMapping("getSection/{caseId}")
    public AjaxResult getSection(@PathVariable("caseId") String caseId){
        return slideService.getSection(caseId);
    }

    @ApiOperation(value = "退回站点")
    @PutMapping("returnSite/{id}")
    public AjaxResult returnSite(@PathVariable("id") String id){
        UpdateWrapper updateWrapper = new UpdateWrapper<>(null)
                .set("status",1)
                .set("update_date",new Date())
                .eq("id",id);
        return emrCaseService.update(updateWrapper) ? AjaxResult.success("退回成功！") : AjaxResult.error("退回失败！");
    }

    @PutMapping("revocation/{caseId}")
    public AjaxResult revocation(@PathVariable String caseId,String reason){
        return emrCaseService.revocation(caseId,reason);
    }
}
